package MyLoader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import ImgCombine.ImageCombine;
import ImgCombine.ObjListStore;
import XmlLoader.Constant;
import XmlLoader.FGObjProcess;
import XmlLoader.FGObjectStructure;
import XmlLoader.FGObjectTrajectoryStructure;
import XmlLoader.Position;

public class MyLoader {
	
	protected FGObjectTrajectoryStructure _001_loadOneFile(String filePath) {
		if(!new File(filePath).exists()) {
			return null;
		}
		FGObjectTrajectoryStructure newFts = new FGObjectTrajectoryStructure();
		FileInputStream fileInputStream = null;
		try{
			fileInputStream = new FileInputStream(filePath);
			BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
			//read trajectory information
			newFts.SET001_nNumberOfVideoFrame(Integer.parseInt(reader.readLine()));
			newFts.SET002_nNumberOfSynopsisFrame(Integer.parseInt(reader.readLine()));
			newFts.SET003_nNumberOfPoint(Integer.parseInt(reader.readLine()));
			newFts.SET004_NobjNewNumIndex(Integer.parseInt(reader.readLine()));
			newFts.SET005_nNumberOfObject(Integer.parseInt(reader.readLine()));
			//read each point information
			int index = 0;
			while(index < newFts.GET002_nNumberOfPoint()) {
				reader.readLine();
				FGObjectStructure newPosObj = new FGObjectStructure();
				newPosObj.SET001_Pointid(index);
				newPosObj.SET002_CenterReal(new Position(Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine())));
				newPosObj.SET003_CenterPredict(new Position(Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine())));
				newPosObj.SET004_PosReal(new Position(Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine())));
				newPosObj.SET005_PosPredict(new Position(Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine())));
				newPosObj.SET006_PosReal_Forward(new Position(Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine())));
				newPosObj.SET007_PosReal_Backward(new Position(Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine())));
				newPosObj.SET008_Width(Integer.parseInt(reader.readLine()));
				newPosObj.SET009_Height(Integer.parseInt(reader.readLine()));
				newPosObj.SET010_tSynopsis(Integer.parseInt(reader.readLine()));
				newPosObj.SET011_tOrigin(Integer.parseInt(reader.readLine()));
				
				newFts.ACT004_addFGObject(newPosObj);
				index = index + 1;
			}
		}
		catch (FileNotFoundException e) {
		    e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return newFts;
	}
	
	public List<FGObjectTrajectoryStructure> ACT001_getListOfObj(String dirPath, List<String> list_objIndex) {
		List<FGObjectTrajectoryStructure> listObj = new ArrayList<FGObjectTrajectoryStructure>();
		String fullFileName = null, editFileName = null, getFormat = null;
		FGObjectTrajectoryStructure objTraj_get = null;
		FGObjectTrajectoryStructure before = null;
		int index = 0;
		while(index < list_objIndex.size())
		{
			//edit to specific file name according to passing object index and rule of file name
			getFormat = FGObjProcess.ACT005_numFormat(list_objIndex.get(index));
			editFileName = Constant.FILEFORM_XML_TRAJ.substring(0);
			editFileName = editFileName.replace(".xml", "");
			editFileName = editFileName.replace("000000", getFormat);
			//set full file name
			fullFileName = dirPath + "/" + editFileName;
			//read file and get the object - FGObjectTrajectoryStructure
			objTraj_get = this._001_loadOneFile(fullFileName);
			//add to list_FGObjectTraStr
			if(objTraj_get != null) {
			    listObj.add(objTraj_get);
			    before = objTraj_get;
			}
			else{
				listObj.add(before);
			}
			if(index % 3000 == 0) {
				System.out.println(index + " is load");
			}
			index = index + 1;
		}
		return listObj;
	}
	
	public static void main(String[] args) {
		//get list-FGObjectTrajectoryStructure
		System.out.println("Start to load target xml file to memory");
		FGObjProcess fgObj = new FGObjProcess(args[1]);
		List<String> list_objIndex = fgObj.ACT004_getStringList(args[0]);
		List<FGObjectTrajectoryStructure> list_traj = new MyLoader().ACT001_getListOfObj(args[1], list_objIndex);
		//show result
		System.out.println(list_traj.size());
	}
}
